package nl.hanze.designpatterns.mvc.model;

import java.io.FileInputStream;
import java.io.ObjectInputStream;
import java.util.Stack;

import nl.hanze.designpatterns.domain.Answer;
import nl.hanze.designpatterns.domain.Question;
import nl.hanze.designpatterns.domain.ThesaurusItem;

public class ThesaurusModel extends Model {

	private Stack<ThesaurusItem> backStack;
	
	public ThesaurusModel() {
		backStack = new Stack<ThesaurusItem>();
	}
	
	public void setCurrent(ThesaurusItem item){
		backStack.push(item);
		notifyViews();
	}
	
	public void back(){
		backStack.pop();
		notifyViews();
	}
	
	public boolean isRootItem(){
		return backStack.size() == 1;
	}

	public ThesaurusItem getCurrent(){
		ThesaurusItem item = null;
		if(backStack.empty()){
			String filename = "thesaurus.ser";
		    // read the object from file
		    // save the object to file
		    FileInputStream fis = null;
		    ObjectInputStream in = null;
		    try {
		      fis = new FileInputStream(filename);
		      in = new ObjectInputStream(fis);
		      backStack.add((ThesaurusItem) in.readObject());
		      in.close();
		    } catch (Exception ex) {
		      ex.printStackTrace();
		    }
		}
		item = backStack.peek();
		return item;
	}
	
}
